/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。
 * 
 * 欢迎到知启蒙网站（https://www.zhiqim.com）购买正版软件，知启蒙还提供许多开源框架和软件。
 * 
 * 1、本软件产品所有源代码受《中华人民共和国著作权法》和其他有关法律、法规的保护，其所有知识产权归湖南知启蒙科技有限公司所有；
 * 2、禁止复制和修改。不得复制修改、翻译或改编本软件所有源代码，或者基于本软件产品创作衍生作品；
 * 3、禁止进行逆向工程。不得对本软件的源代码进行逆向工程、反编译或试图以其他方式发现软件的源代码；
 * 4、个别授权：如需进行商业性的销售、复制、分发，包括但不限于软件销售、预装、捆绑等，必须获得知启蒙的书面授权和许可；
 * 5、保留权利：本注释未明示授权的其他一切权利仍归知启蒙所有，任何人使用其他权利时必须获得知启蒙的书面同意。
*/
package net.redxyz.repost.manage.action.app;

import java.util.List;

import net.redxyz.top.dbo.sys.SysPopup;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.annotation.AnIntercept;
import org.zhiqim.httpd.context.extend.StdSwitchAction;
import org.zhiqim.httpd.validate.ones.IsNotEmpty;
import org.zhiqim.httpd.validate.ones.IsNumericLen;
import org.zhiqim.kernel.util.Ids;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.orm.dbo.Updater;

public class PopupAction extends StdSwitchAction
{

    protected void validateId(HttpRequest request)
    {
        request.addValidate(new IsNotEmpty("popupId", "请选择一个选项"));
    }

    protected void validateForm(HttpRequest request)
    {
        request.addValidate(new IsNotEmpty("popupName", "弹出窗口名称不能为空"));
        request.addValidate(new IsNumericLen("popupWidth", "弹出窗口宽度必须为数字，且不能超过4位", 1, 4));
        request.addValidate(new IsNumericLen("popupHeight", "弹出窗口高度必须为数字，且不能超过4位", 1, 4));
        request.addValidate(new IsNumericLen("popupIndex", "优先级别必须为数字，且不能超过4位", 1, 4));
        request.addValidate(new IsNumericLen("lowestVersion", "最低版本号必须为数字，且不能超过4位", 1, 4));
        request.addValidate(new IsNumericLen("highestVersion", "最高版本号必须为数字，且不能超过4位", 1, 4));
        request.addValidate(new IsNumericLen("expireDay", "到期天数必须为数字，且不能超过4位", 1, 4));
    }

    protected void list(HttpRequest request) throws Exception
    {
        List<SysPopup> popupList = ORM.table().list(SysPopup.class, new Selector().addOrderbyAsc("popupIndex"));
        request.setAttribute("popupList", popupList);
    }

    protected void add(HttpRequest request) throws Exception
    {
        request.setResponsePrivateCache();
    }

    protected void modify(HttpRequest request) throws Exception
    {
        request.setResponsePrivateCache();
        String popupId = request.getParameter("popupId");
        SysPopup popup = ORM.table().item(SysPopup.class, popupId);
        if (popup == null)
        {
            request.returnHistory("弹出窗口信息不存在，请重新选择");
            return;
        }

        request.setAttribute("popup", popup);
    }

    protected void insert(HttpRequest request) throws Exception
    {
        SysPopup popup = request.getParameter(SysPopup.class);
        String popupFtl = request.getParameterNoFilter("popupFtl");
        String popupId = Ids.uuid();
        popup.setPopupId(popupId);
        popup.setPopupFtl(popupFtl);

        ORM.table().insert(popup);
    }

    protected void update(HttpRequest request) throws Exception
    {
        SysPopup popup = request.getParameter(SysPopup.class);
        String popupId = popup.getPopupId();
        String popupFtl = request.getParameterNoFilter("popupFtl");
        popup.setPopupFtl(popupFtl);

        Updater updater = new Updater();
        updater.addMaybe("popupId", popupId);
        updater.addField("popupName", popup.getPopupName());
        updater.addField("popupWidth", popup.getPopupWidth());
        updater.addField("popupHeight", popup.getPopupHeight());
        updater.addField("popupStatus", popup.getPopupStatus());

        updater.addField("popupIndex", popup.getPopupIndex());
        updater.addField("lowestVersion", popup.getLowestVersion());
        updater.addField("highestVersion", popup.getHighestVersion());
        updater.addField("expireDay", popup.getExpireDay());
        updater.addField("popupFtl", popupFtl);

        ORM.table().update(SysPopup.class, updater);
    }

    protected void delete(HttpRequest request) throws Exception
    {
        String popupId = request.getParameter("popupId");

        ORM.table().delete(SysPopup.class, popupId);
    }

    /**
     * 上下移动条件
     * 
     * @param request
     * @param pos 当前优先级别item_index+1
     * @param arrow 2表示向上移动，3表示向下
     * @throws Exception
     */
    @AnIntercept("chkZmrLogin")
    public String doMoveItem(HttpRequest request, int pos, int arrow) throws Exception
    {
        List<SysPopup> popupList = ORM.table().list(SysPopup.class, new Selector().addOrderbyAsc("popupIndex"));
        // 查询当前移动的条件
        SysPopup curPopup = popupList.get(pos - 1);

        if (arrow == 2)
        {// 向上移动一位

            // 修改当前条件优先级别
            String curPopupId = curPopup.getPopupId();
            Updater curUpd = new Updater();
            curUpd.addMaybe("popupId", curPopupId);
            curUpd.addField("popupIndex", pos - 1);
            ORM.table().update(SysPopup.class, curUpd);

            // 修改上一个条件
            SysPopup prePopup = popupList.get(pos - 2);
            String prePopupId = prePopup.getPopupId();
            Updater preUpd = new Updater();
            preUpd.addMaybe("popupId", prePopupId);
            preUpd.addField("popupIndex", pos);
            ORM.table().update(SysPopup.class, preUpd);

            // 修改缓存
            prePopup.setPopupIndex(pos);
        }
        else
        {// 向下移动一位
            // 修改当前条件优先级别
            String curPopupId = curPopup.getPopupId();
            Updater curUpd = new Updater();
            curUpd.addMaybe("popupId", curPopupId);
            curUpd.addField("popupIndex", pos + 1);
            ORM.table().update(SysPopup.class, curUpd);

            // 修改下一个条件
            SysPopup nextPopup = popupList.get(pos);
            String nextPopupId = nextPopup.getPopupId();
            Updater nextUpd = new Updater();
            nextUpd.addMaybe("popupId", nextPopupId);
            nextUpd.addField("popupIndex", pos);
            ORM.table().update(SysPopup.class, nextUpd);
        }

        return "处理成功";
    }

}
